<?php
header("Content-Type: text/html;charset=utf-8");
import('@.ORG.UploadFile');
import('@.ORG.Image');
class CommonAction extends Action{

    function _initialize() {
		$this->_checkcode();
    	$password = $_SESSION['password'];
		$data['username'] = $_SESSION['username'];
		$data['password'] = encrypt($password);
		if (empty($_SESSION['password']) and empty($_SESSION['username']))
		{
			$this->redirect('Admin/login');

		}
		$m = M('admin');
		$admin = $m->where($data)->find();
		if($admin == null){
			$this->assign('jumpUrl',__APP__.'/Admin/login/');
			$this->assign('waitSecond','3');
			$this->error("登录失败，账号或密码错误");
		}
    }


	public function _isempty($key,$str){
		if(empty($key)){
			$this->error($str);
		}else{
			return $key;
		}
	}




    protected function _listpage($model, $map, $sortBy = '', $asc = false ,$real=null) {
		//排序字段 默认为主键名
		if (isset ( $_REQUEST ['_order'] )) {
			$order = $_REQUEST ['_order'];
		} else {
			$order = ! empty ( $sortBy ) ? $sortBy : $model->getPk ();
		}
		//排序方式默认按照倒序排列
		//接受 sost参数 0 表示倒序 非0都 表示正序
		if (isset ( $_REQUEST ['_sort'] )) {
			$sort = $_REQUEST ['_sort'] ? 'asc' : 'desc';
		} else {
			$sort = $asc ? 'asc' : 'desc';
		}
		//取得满足条件的记录数
		$keyid=$model->getpk();
		$count = $model->where ( $map )->count ($keyid);
//		echo $model->getlastsql();
		if ($count > 0) {
			import ( "ORG.Util.Page" );
			//创建分页对象
			if (! empty ( $_REQUEST ['listRows'] )) {
				$listRows = $_REQUEST ['listRows'];

			} else {
				$listRows = '15';
			}

			$p = new Page ( $count, $listRows );
			//分页查询数据

			$voList = $model->where($map)->order( "`" . $order . "` " . $sort)->limit($p->firstRow . ',' . $p->listRows)->select ( );
//			echo $model->getlastsql();
			//分页跳转的时候保证查询条件
			if($real == null){
				foreach ( $map as $key => $val ) {
					if (! is_array ( $val )) {
						$p->parameter .= "&$key=" . urlencode ( $val ) . "&";
					}
				}
			}else{
				foreach ( $real as $key => $val ) {
					if (! is_array ( $val )) {
						$p->parameter .= "&$key=" . urlencode ( $val ) . "&";
					}
				}
			}

			//分页显示
			$page = $p->show ();
			//列表排序显示
			$sortImg = $sort; //排序图标
			$sortAlt = $sort == 'desc' ? '升序排列' : '倒序排列'; //排序提示
			$sort = $sort == 'desc' ? 1 : 0; //排序方式
			//模板赋值显示

			$this->assign ( 'list', $voList );
			$this->assign ( 'sort', $sort );
			$this->assign ( 'order', $order );
			$this->assign ( 'sortImg', $sortImg );
			$this->assign ( 'sortType', $sortAlt );
			$this->assign ( "page", $page );
		}

		Cookie( '_currentUrl_',$_SERVER["REQUEST_URI"]);
		return;
	}



  	//上传图片
	function uploadimg($dir,$type='uniqid'){
		$upload=new UploadFile();
		$upload->maxSize=1024*20*1024;
		$upload->savePath='./uploads/'.$dir;
		$upload->saveRule= $type;
		$upload->uploadReplace=true;
		$upload->allowExts=array('jpg','jpeg','png','gif');     //准许上传的文件后缀

		$upload->allowTypes=array('image/jpeg','image/pjpeg','image/png','image/gif','image/x-png');//准许上传的文件类型
		$upload->imageClassPath = '@.ORG.Image';
		if($upload->upload())
		{
			$info=$upload->getUploadFileInfo();
			Image::thumb($info[0]['savepath'].'/'.$info[0]['savename'], $info[0]['savepath'].'/logo/osu');
			return $info;
		}else{
			$this->error($upload->getErrorMsg());
		}
	}

	private function _checkcode(){
		if($_SESSION['code'] != null && $_SESSION['code'] !=""){
			if($_SESSION['code'] != C('SYSTEM_CODE')){
				$this->error("您不能登录该系统");
	    	}
		}
	}

}
?>